﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AspNetExpressInformationManageSystem.Models;
using IdentitySample.Models;

namespace AspNetExpressInformationManageSystem.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ExpressAnalysisController : Controller
    {
        private ExpressDbContext db = new ExpressDbContext();
        private ApplicationDbContext app = new ApplicationDbContext();

        // GET: ExpressAnalysis
        public ActionResult Index()
        {
            return View(db.ExpressAnalysis.ToList());
        }

        // GET: ExpressAnalysis/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressAnalysis expressAnalysis = db.ExpressAnalysis.Find(id);
            if (expressAnalysis == null)
            {
                return HttpNotFound();
            }
            return View(expressAnalysis);
        }

        // GET: ExpressAnalysis/Create
        public ActionResult Create()
        {
            var expressGoodsInput = db.ExpressGoods.Where(s => s.Date.Month == DateTime.Now.Month).Sum(s => s.Cost);
            var expressVehicleOilOutput = 0;
            var expressVehicleMaintenanceOutput = 0;
            var usersSalary = app.Users.Select(s => s.Salary).Sum();

            foreach (ExpressVehicle vehicle in db.ExpressVehicles.ToList())
            {
                var monthDispatchsCount = db.ExpressDispatches
                    .Where(s => s.Date.Month == DateTime.Now.Month 
                        && s.ExpressVehicleID == vehicle.ID)
                    .Count();
                if (monthDispatchsCount != 0)
                {
                    expressVehicleOilOutput += vehicle.OilCost;
                    expressVehicleMaintenanceOutput += vehicle.Maintenance;
                }
            }
            return View(new ExpressAnalysis() { Date = DateTime.Now.Date, GoodsCash = expressGoodsInput, VehicleOilCost = expressVehicleOilOutput, VehicleMaintenance = expressVehicleMaintenanceOutput, Salary = usersSalary });
        }

        // POST: ExpressAnalysis/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Date,GoodsCash,VehicleOilCost,VehicleMaintenance,Salary")] ExpressAnalysis expressAnalysis)
        {
            if (ModelState.IsValid)
            {
                db.ExpressAnalysis.Add(expressAnalysis);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(expressAnalysis);
        }

        // GET: ExpressAnalysis/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressAnalysis expressAnalysis = db.ExpressAnalysis.Find(id);
            if (expressAnalysis == null)
            {
                return HttpNotFound();
            }
            return View(expressAnalysis);
        }

        // POST: ExpressAnalysis/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Date,GoodsCash,VehicleOilCost,VehicleMaintenance,Salary")] ExpressAnalysis expressAnalysis)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expressAnalysis).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(expressAnalysis);
        }

        // GET: ExpressAnalysis/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressAnalysis expressAnalysis = db.ExpressAnalysis.Find(id);
            if (expressAnalysis == null)
            {
                return HttpNotFound();
            }
            return View(expressAnalysis);
        }

        // POST: ExpressAnalysis/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ExpressAnalysis expressAnalysis = db.ExpressAnalysis.Find(id);
            db.ExpressAnalysis.Remove(expressAnalysis);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
